package com.cycrix.tcp;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class TcpDummy extends TcpObject {

	private int _size;
	
	public TcpDummy() {
	}
	
	public TcpDummy(int size) {
		_size = size;
	}
	
	@Override
	public int GetSize() {
		return _size;
	}

	@Override
	public int GetStream(byte[] buffer, int size) {
		ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
		
		// 1. write code field
		int code = TcpConnection.ENUM_DUMMY;
		byteBuffer.putInt(4, code);

		// 2. write text field
		// Nothing to write

		// 3. Write Size
		byteBuffer.putInt(0, _size);

		// 4. Write checksum
		byteBuffer.putInt(8, code ^ _size);
		
		return _size;
	}

	@Override
	public TcpObject FromStream(byte[] buffer) {
		ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
		byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
		
		_size = byteBuffer.getInt(0);
		
		return this;
	}

	@Override
	public int GetType() {
		return TcpConnection.ENUM_DUMMY;
	}

}
